/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.myapp.c0910g.entity;

import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

/**
 *
 * @author BachLe
 */
@Stateless
public class PaymentDetailsFacade extends AbstractFacade<PaymentDetails> {
    @PersistenceContext(unitName = "RadioCabsProject-ejbPU")
    private EntityManager em;

    protected EntityManager getEntityManager() {
        return em;
    }

    public PaymentDetailsFacade() {
        super(PaymentDetails.class);
    }

    public PaymentDetails getPaymentDetailsByUserID(Integer userid){
        String eql = "SELECT p FROM PaymentDetails p WHERE p.userRadioCab.urcid = :userid";
        return (PaymentDetails) em.createQuery(eql).setParameter("userid", userid).getSingleResult();
    }

    public List<ObjectData> getAllUser_CheckBan_SendEmailByStatus(){
        String eql = "SELECT new com.entity.ObjectData(p.userRadioCab.urcid,"
                + "p.userRadioCab.uRCName,"
                + "p.userRadioCab.paymentType.paymentTypeID,"
                + "p.userRadioCab.paymentType.paymentTypeName,"
                + "p.userRadioCab.uRCEmail,"
                + "p.userRadioCab.paymentType.paymentBy,"
                + "p.pDStartDate)"
                + "FROM PaymentDetails p "
                + "WHERE p.userRadioCab.uRCStatus = 'true' "
                + "and p.pDStatus = 'true' "
                + "and p.userRadioCab.uRCName <> 'admin'";
        return em.createQuery(eql).getResultList();
    }
    //update user not pay money
    public int updateStatusNotPay(Integer userid){
        String eql = "UPDATE PaymentDetails p SET p.pDStatus = false WHERE p.userRadioCab.urcid = :userid";
        return em.createQuery(eql).setParameter("userid", userid).executeUpdate();
    }
    
    public List<PaymentDetails> getAllRecordOfListing(){
        String eql = "SELECT p FROM PaymentDetails p WHERE p.userRadioCab.userRole.roleName = :roleName";
        return em.createQuery(eql).setParameter("roleName", "Listing").getResultList();
    }

    public List<PaymentDetails> getAllRecordOfDriver(){
        String eql = "SELECT p FROM PaymentDetails p WHERE p.userRadioCab.userRole.roleName = :roleName";
        return em.createQuery(eql).setParameter("roleName", "Driver").getResultList();
    }

    public List<PaymentDetails> getAllRecordOfAdvertise(){
        String eql = "SELECT p FROM PaymentDetails p WHERE p.userRadioCab.userRole.roleName = :roleName";
        return em.createQuery(eql).setParameter("roleName", "Advertise").getResultList();
    }
}
